package com.boang.web;

import com.boang.po.Cla;
import com.boang.po.Page;
import com.boang.service.ClaService;
import com.boang.service.ClaServiceImpl;
import jakarta.servlet.RequestDispatcher;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.HttpSession;

import java.io.IOException;
import java.util.List;

/*
  @author: 13813
  @date: 2024/7/15 9:41
*/
@WebServlet("/cla")
public class ClaServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        HttpSession session = req.getSession();
//        String useranme = (String) session.getAttribute("abc");
//        if (useranme != null || !useranme.equals("")) {
            ClaServiceImpl claService = new ClaServiceImpl();
            int pageNum = Integer.parseInt(req.getParameter("pageNum"));
            int pageSize = Integer.parseInt(req.getParameter("pageSize"));
            Page<Cla> page = claService.getClaList(pageNum, pageSize);
            req.setAttribute("page", page);

            RequestDispatcher dispatcher = req.getRequestDispatcher("cla.jsp");
            dispatcher.forward(req, resp);
//        } else {
//            resp.sendRedirect("index.jsp");
//        }
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String name = req.getParameter("name");
        int num = Integer.parseInt(req.getParameter("num"));
        ClaServiceImpl claService = new ClaServiceImpl();
        Cla cla = new Cla(1, name, num);
        claService.addCla(cla);
        //跳转成功页面
        RequestDispatcher dispatcher = req.getRequestDispatcher("success.jsp");
        dispatcher.forward(req, resp);
    }
}
